package com.jshcbd.modules.asset.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshcbd.framework.vo.Result;
import com.jshcbd.framework.query.QueryGenerator;
import com.jshcbd.modules.asset.dto.InStorageItemVo;
import com.jshcbd.modules.asset.entity.InStoreageEntity;
import com.jshcbd.modules.asset.entity.InStorageItemEntity;
import com.jshcbd.modules.asset.service.IInStoreageItemService;
import com.jshcbd.modules.asset.service.IInStoreageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Api(tags="资产入库")
@RestController
@RequestMapping("/instoreage")
public class InStoreageController {

    @Autowired
    private IInStoreageService inStoreageService;

    @Autowired
    private IInStoreageItemService assetItemService;


    @ApiOperation("主表列表")
    @GetMapping( value = {"/list","/list/{state}"})
    public Result<IPage<InStoreageEntity>> queryPageList(InStoreageEntity assetEntity,
                                                         @PathVariable(required = false) Integer state,
                                                         @RequestParam(defaultValue="1") Integer pageNo,
                                                         @RequestParam(defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<InStoreageEntity>> result = new Result<>();
        QueryWrapper<InStoreageEntity> queryWrapper = QueryGenerator.initQueryWrapper(assetEntity, req.getParameterMap());

        if(state !=null ){
            queryWrapper.and( q -> q.in("state",state));
        }else{
            queryWrapper.and( q -> q.in("state",1,2,4));
        }
        Page<InStoreageEntity> page = new Page<InStoreageEntity>(pageNo, pageSize);
        IPage<InStoreageEntity> pageList = inStoreageService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setData(pageList);
        log.info(pageList.toString());
        return result;
    }

    @ApiOperation("主表新增")
    @PostMapping("/add")
    public Result add(@RequestBody InStoreageEntity assetEntity) {

        try {
            inStoreageService.save(assetEntity);
            return Result.ok("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }

    @ApiOperation("主表编辑")
    @PutMapping("/edit")
    public Result edit(@RequestBody InStoreageEntity assetEntity) {
        try {
            InStoreageEntity entity = inStoreageService.getById(assetEntity.getId());
            if(entity==null) {
                return Result.error("未找到对应实体");
            }else {
                inStoreageService.updateById(assetEntity);
                return Result.ok("修改成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }

    @ApiOperation("主表删除")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam String id) {
        try {
            this.inStoreageService.deleteById(id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation("明细列表")
    @GetMapping("/itemList")
    public Result<IPage<InStorageItemEntity>> itemList(@RequestParam String  mainId, @PathVariable(required = false) Integer state,
                                                       @RequestParam(defaultValue="1") Integer pageNo,
                                                       @RequestParam(defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<InStorageItemEntity>> result = new Result();
        Page<InStorageItemEntity> page = new Page(pageNo, pageSize);
        IPage<InStorageItemEntity> pageList = assetItemService.page(page,new QueryWrapper<InStorageItemEntity>()
                .lambda()
                .eq(InStorageItemEntity::getMainId,mainId));
        result.setSuccess(true);
        result.setData(pageList);
        return result;
    }


    @ApiOperation("添加明细")
    @PostMapping("/addItem")
    public Result addItem(@RequestBody InStorageItemVo inStorageItemVo) {
        try {
            assetItemService.addStoreageItem(inStorageItemVo);
            return Result.ok("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }


    @ApiOperation("明细编辑")
    @PutMapping("/editItem")
    public Result editItem(@RequestBody InStorageItemEntity assetItemEntity) {
        try {
            InStorageItemEntity entity = assetItemService.getById(assetItemEntity.getId());
            if(entity==null) {
                return Result.error("未找到对应实体");
            }else {
                assetItemService.updateById(assetItemEntity);
                return Result.ok("修改成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }


    @ApiOperation("明细删除")
    @DeleteMapping("/deleteItem")
    public Result<?> deleteItem(@RequestParam String id) {
        try {
            inStoreageService.deleteItemById(id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation("修改状态（(1:制定/2:提交/3:生效/4:驳回)）")
    @PutMapping("/changeState")
    public Result changeState(@RequestBody JSONObject object) {
        try {

            String id = object.getString("id");
            Integer type = object.getInteger("type");
            inStoreageService.changeStateById(id,type);
            return Result.ok("操作成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }



}
